package larpplanner.gui;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import larpplanner.database.CharUniverse;
import larpplanner.database.FicCharacter;
import larpplanner.database.Universe;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;

public class UniverseCharacterPanel extends org.eclipse.swt.widgets.Composite{

	private GUISafeLARPManager mngr = GUISafeLARPManager.get();

	private CharWin charWin;
	
	private Map<String, Long> charNamesAndIds;
	private FicCharacter ficChar;
	
	private Composite univCharHolder;
	private Combo charNameCombo;
	private Combo univNameCombo;
	
	public UniverseCharacterPanel(Composite parent, int style, CharWin charWin) {
		super(parent, style);
		this.charWin = charWin;
		initGUI(parent);
		initLogic();
	}

	public int getCharComboIndex() {
		return charNameCombo.getSelectionIndex();
	}
	
	public Long getCharId() {
		int selectedIndex = charNameCombo.getSelectionIndex();
		if(selectedIndex == -1)
			return null;
		else
			return charNamesAndIds.get(charNameCombo.getItem(selectedIndex));
	}
	
	public String getCharName() {
		return charNameCombo.getText();
	}
	
	private void initLogic() {
		// get all universes
		mngr.getAll(Universe.class, new SafeCallback<List<Universe>>() {
			public void onReturn(List<Universe> univList) {
			    
				String[] univNamesArray = new String[univList.size()+1];
				univNamesArray[0] = "<select universe>";
				for (int i = 1; i < univNamesArray.length; i++) 
                    univNamesArray[i] = univList.get(i-1).name;
				univNameCombo.setItems(univNamesArray);
				
				// only after comboBox loaded, set the selection
				if (ficChar != null)
				    univNameCombo.setText(ficChar.getUniverses().get(0)); //first universe the character appears in
				else
				    univNameCombo.select(0);
			}
		});
		
		charNameCombo.setEnabled(false);
		// when universe is selected, get all the characters in that universe
		univNameCombo.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				mngr.getCharacters(univNameCombo.getItem(univNameCombo.getSelectionIndex()), new SafeCallback<List<CharUniverse>>() {
					public void onReturn(List<CharUniverse> returnedList) {
						charNamesAndIds = new TreeMap<String, Long>();
						for(CharUniverse cu : returnedList)
							charNamesAndIds.put(cu.getCharName(), cu.getCharId());
						String[] charNamesArray = charNamesAndIds.keySet().toArray(new String[0]);
						charNamesArray = addToStringArr(charNamesArray, "<select character>");
						charNameCombo.setItems(charNamesArray);
						charNameCombo.setText(charNamesArray[0]);
						if(charWin != null){
							charWin.setCharNameLbl(charNameCombo.getItem(charNameCombo.getSelectionIndex()));
							charWin.setOkBtnEnabled(false);
						}
						charNameCombo.setEnabled(true);
					}
				});
				
			}
		});
		
		if(charWin != null){
			charNameCombo.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					charWin.setCharName(charNameCombo.getText());
					charWin.setCharNameLbl(charNameCombo.getText());
					mngr.get(FicCharacter.class, charNamesAndIds.get(charNameCombo.getText()), new SafeCallback<FicCharacter>() {
						public void onReturn(FicCharacter ficChar) {
							charWin.setCharDetailsText(ficChar.getDescriptionFromWeb());
						}
					});
					if(charNameCombo.getSelectionIndex() > 0 && charWin.getPlayerComboIndex() > 0)
						charWin.setOkBtnEnabled(true);
					else
						charWin.setOkBtnEnabled(false);
				}
			});
		}
	}
	
	private void initGUI(Composite parent) {
		{
			univCharHolder = new Composite(parent, SWT.NONE);
			RowLayout univCharHolderLayout = new RowLayout(org.eclipse.swt.SWT.HORIZONTAL);
			GridData univCharHolderData = new GridData();
			univCharHolder.setLayoutData(univCharHolderData);
			univCharHolder.setLayout(univCharHolderLayout);
			{
			    Label uniLbl = new Label(univCharHolder, SWT.None);
			    uniLbl.setText("Universe:");
			}
			{
				univNameCombo = new Combo(univCharHolder, SWT.READ_ONLY);
				RowData univNameComboLData = new RowData();
                univNameComboLData.width = 140;
				univNameCombo.setLayoutData(univNameComboLData);
			}
			{
                Label charLbl = new Label(univCharHolder, SWT.None);
                charLbl.setText("Character:");
            }
			{
				charNameCombo = new Combo(univCharHolder, SWT.READ_ONLY);
				RowData charNameComboLData = new RowData();
                charNameComboLData.width = 137;
				charNameCombo.setLayoutData(charNameComboLData);
			}
		}
	}
	
	private String[] addToStringArr(String[] a, String s) {
		String[] res = new String[a.length + 1];
		res[0] = s;
		for(int i = 1 ; i < res.length ; i++)
			res[i] = a[i-1];
		return res;
	}
	
}
